	$(window).load(function() {
	    $('div .post').each(function() {
	        $(this).height($(this).find('img').height()+130);
	    });
		  
	});
	
	function loadColour(id) {
		var voteVal = parseInt($('#voteTotal_'+id).html());
		  if(parseInt(voteVal) < 0) {
			  $('#voteTotal_'+id).css({'color':'red'});
		  }else if (parseInt(voteVal) > 0) {
			  $('#voteTotal_'+id).css({'color':'green'});
		  }
	}
	
	function processVotes(id,vote_type) {
	  var newVal = parseInt($('#voteTotal_'+id).html());
  	  if (vote_type == 'up') { 
  		  newVal = newVal + 1;
  		  $('#upvote_'+id).attr('disabled', true);
  		  $('#upvote_'+id).attr('style', 'opacity: 0.4;');
  		  if ($('#downvote_'+id).attr('disabled')) {
  			  newVal = newVal + 1;
      		  $('#downvote_'+id).removeAttr('disabled');
      		  $('#downvote_'+id).removeAttr('style');
  		  }
  		  if(parseInt(newVal) > 0) {
				  $('#voteTotal_'+id).css({'color':'green'});
			  } 
  		  $('#voteTotal_'+id).text(newVal);
  	  } else if (vote_type == 'down') {
  		  newVal = newVal - 1;
  		  $('#downvote_'+id).attr('disabled', true);
  		  $('#downvote_'+id).attr('style', 'opacity: 0.4;'); 
  		  if ($('#upvote_'+id).attr('disabled')) {
  			  newVal = newVal - 1;
      		  $('#upvote_'+id).removeAttr('disabled');
      		  $('#upvote_'+id).removeAttr('style');
  		  }
			  if(parseInt(newVal) < 0) {
				  $('#voteTotal_'+id).css({'color':'red'});
			  }
  		  $('#voteTotal_'+id).text(newVal);
  	  }
	}
	
	var oneClick = false;
	
	 $(document).ready(function(){
		  $(".vote").click(function(){
		   if (oneClick == false) {
				   
			   oneClick = true;
			   var id = $(this).attr('id').split('_')[1];
			   
			   var vote_type = $(this).hasClass('up') ? 'up' : 'down';
			      $.ajax({
			          url: "/voting/",
			          type: 'POST',
			          data: {id: id, type: vote_type},
			          success: function() {
			        	  processVotes(id,vote_type);
			        	  oneClick = false;
			          }
			      });
			      return false;
		   }
		   });
	 });

	 $(window).on('scroll', function () {
	     var scrollTop     = $(window).scrollTop(),
	         elementOffset = $('#adRef').offset().top,
	         distance      = (elementOffset - scrollTop);
	     if (distance <= 50) {
	    	 $('#adRef').addClass('fixed');
	     } 
	     
	     var aboveAdd = $('#topPosts').offset().top,
	     		addHeight = $('#adRef').height(),
	     		newDist = ((aboveAdd + addHeight)- scrollTop);
	     if(newDist > 50) {
	    	 $('#adRef').removeClass('fixed');
	     }
	 });
	